/**
 * Project: springyounger.lottery.web
 * 
 * File Created at 2010-4-4
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyouner.lottery.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.displaytag.util.ParamEncoder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.springyouner.lottery.tools.LotteryUtil;
import com.springyounger.lottery.dal.dataobject.LotteryQuestion;
import com.springyounger.lottery.dal.dataobject.LotteryQuestionResult;
import com.springyounger.lottery.dal.dataobject.ResultInfo;
import com.springyounger.lottery.service.user.QuestionService;

/**
 * 题目管理控制器
 * 
 * @author springyounger
 */
public class AdminQuestionController extends MultiActionController {

    private QuestionService questionService;

    public ModelAndView displayInsert(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        //显示增加页面
        return new ModelAndView("admin/questionInsertAndUpdate");
    }

    @SuppressWarnings("unchecked")
    public ModelAndView findByAll(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Map model = new HashMap();
        //页数的参数名
        String pageIndexName = new ParamEncoder("row")
                .encodeParameterName(org.displaytag.tags.TableTagParameters.PARAMETER_PAGE);
        // 每页显示的条数 
        int pageSize = 10;
        // 当前页
        int pageIndex = LotteryUtil.isEmpty(request.getParameter(pageIndexName)) ? 0 : (Integer
                .parseInt(request.getParameter(pageIndexName)) - 1);
        //总记录
        int resultSize = questionService.getAllQuestionCount();
        List<LotteryQuestion> questionList = questionService.getAllQuestion(pageIndex, pageSize);

        model.put("resultSize", resultSize);
        model.put("questionList", questionList);
        return new ModelAndView("admin/questionList", model);
    }

    @SuppressWarnings("unchecked")
    public ModelAndView findById(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Map model = new HashMap();

        String id = request.getParameter("id");
        if (LotteryUtil.isEmpty(id) || !LotteryUtil.isNumeric(id)) {
            return findByAll(request, response);
        }
        int questionId = Integer.parseInt(id);
        LotteryQuestion question = questionService.getQuestionById(questionId);

        List<LotteryQuestionResult> reslutList = questionService.getQuestinResult(question
                .getQuestionId());
        String reslutSn = questionService.getResultById(question.getQuestionId());

        model.put("question", question);
        model.put("reslutList", reslutList);
        model.put("reslutSn", reslutSn);

        return new ModelAndView("admin/questionInsertAndUpdate", model);
    }

    @SuppressWarnings("unchecked")
    public ModelAndView insertAndUpdate(HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        Map params = new HashMap();
        String resultSn[] = { "A", "B", "C", "D" };

        //题目内容
        String questionType = request.getParameter("questionType");
        String content = request.getParameter("content");

        //答案内容
        String resultA = request.getParameter("resultA");
        String resultB = request.getParameter("resultB");
        String resultC = request.getParameter("resultC");
        String resultD = request.getParameter("resultD");
        String result[] = { resultA, resultB, resultC, resultD };

        //标准答案
        String isSolution = request.getParameter("isSolution");

        String questionId = request.getParameter("id");

        LotteryQuestion question = new LotteryQuestion();
        question.setQuestionType(questionType);
        question.setQuestionLevel("0");
        question.setQuestionContent(content);

        //答案列表
        List<LotteryQuestionResult> resultList = new ArrayList<LotteryQuestionResult>();

        //创建答案对象
        for (int i = 0; i < resultSn.length; i++) {
            LotteryQuestionResult questionResult = new LotteryQuestionResult();
            //设置序号
            questionResult.setResultSn(resultSn[i]);
            questionResult.setContent(result[i]);
            //设置正确答案
            if (isSolution.equals(resultSn[i])) {
                questionResult.setIsSolution("y");
            } else {
                questionResult.setIsSolution("n");
            }
            resultList.add(questionResult);
        }

        ResultInfo info = new ResultInfo();

        if (questionId.equals("") || questionId.equals("0")) {
            //创建
            boolean isExist = questionService.isExistName(content);
            if (isExist) {
                params.put("question", question);
                params.put("message", "题目已经存在");
                //params.put("resultList", resultList);
                return new ModelAndView("admin/questionInsertAndUpdate", params);
            }
            info = questionService.insertQuestionAndResult(question, resultList);
            if (info.equals(ResultInfo.RESULT_FAILED)) {
                return new ModelAndView("admin/questionInsertAndUpdate");
            }
        } else {
            LotteryQuestion verifyQuestion = questionService.getQuestionById(Integer
                    .parseInt(questionId));
            //与原来名称不一致,进行题目重复检查
            if (!content.equals(verifyQuestion.getQuestionContent())) {
                boolean isExist = questionService.isExistName(content);
                if (isExist) {
                    params.put("question", question);
                    params.put("message", "题目已经存在");
                    //params.put("resultList", resultList);
                    return new ModelAndView("admin/questionInsertAndUpdate", params);
                }
            }
            //修改
            question.setQuestionId(Integer.parseInt(questionId));
            info = questionService.updateQuestionAndResult(question, resultList);
            if (info.equals(ResultInfo.RESULT_FAILED)) {
                return new ModelAndView("admin/questionInsertAndUpdate");
            }
        }
        return findByAll(request, response);
    }

    public ModelAndView delete(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");
        if (LotteryUtil.isEmpty(id) || !LotteryUtil.isNumeric(id)) {
            return findByAll(request, response);
        }
        int questionId = Integer.parseInt(id);
        questionService.deleteQuestionById(questionId);
        return findByAll(request, response);
    }

    /**
     * @param questionService the questionService to set
     */
    public void setQuestionService(QuestionService questionService) {
        this.questionService = questionService;
    }
}
